﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileWebsiteGenerator.Entities;
using MobileWebsiteGenerator;
using MobileWebsiteGenerator.Models;
using System.IO;

namespace MobileWebsiteGenerator.Areas.Admin.Controllers
{
    public class ThemeDetailsController : Controller
    {
        private Mobile_appContext db = new Mobile_appContext();

        //
        // GET: /Admin/ThemeDetails/

        public ViewResult Index(int id)
        {
            MvcApplication._MainCustomer.Website = db.Websites.Where(w => w.web_id == 1).FirstOrDefault();
            MvcApplication.CustomerID = id;
            MvcApplication.IscreateNew = true;
            var themes = db.WebthemeRels.Where(w => w.Web_id == 1).Select(t => t.Theme_id);
            return View(db.ThemeDetails.Where(t => themes.Contains(t.theme_id)).ToList());
        }
        public ActionResult ChangeSetting(int id)
        {


            return View(MvcApplication._MainCustomer);
        }


        [HttpPost]
        public ActionResult ChangeSetting(MainViewModel CurrentTheme)
        {




            if (ModelState.IsValid)
            {
                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.Headercolor = CurrentTheme.MyTheme.Headercolor;
                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.HeaderImage = CurrentTheme.MyTheme.HeaderImage;
                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.HeaderText = CurrentTheme.MyTheme.HeaderText;

                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.Image = CurrentTheme.MyTheme.Image;
                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.Footertext = CurrentTheme.MyTheme.Footertext;

                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.backgroundColor = CurrentTheme.MyTheme.backgroundColor;
                MobileWebsiteGenerator.MvcApplication._MainCustomer.MyTheme.backgroundImage = CurrentTheme.MyTheme.backgroundImage;
                MvcApplication.IscreateNew = true;




                return RedirectToAction("EditWebsite", "AdminCustomer", MobileWebsiteGenerator.MvcApplication._MainCustomer);

            }
            return View();
        }

        public string Upload(HttpPostedFileBase fileData)
        {

            var fileName = this.Server.MapPath("~/Uploads/" + System.IO.Path.GetFileName(fileData.FileName));
            fileData.SaveAs(fileName);

            return Path.GetFileName(fileName);
        }


        public ActionResult Edit(int id)
        {
            MvcApplication._MainCustomer.MyTheme = db.ThemeDetails.Where(t => t.theme_id == id).FirstOrDefault();


            MvcApplication._MainCustomer.ContentPages = db.Pages.Where(p => db.ThemePageRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Page_id).Contains(p.page_id)).ToList();
            MvcApplication._MainCustomer.PictureGallery = db.pictureGalleries.Where(p => db.ThemePictureRELs.Where(c => c.Theme_id == MvcApplication._MainCustomer.MyTheme.theme_id).Select(x => x.Picture_id).Contains(p.picture_id)).ToList();
            return Redirect(Url.Action("EditWebsite", "AdminCustomer"));

        }

        //
        // GET: /Admin/ThemeDetails/Details/5



        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}